package com.systemsjr.jrbbf.expense;

import net.rim.device.api.command.Command;
import net.rim.device.api.command.CommandHandler;
import net.rim.device.api.command.ReadOnlyCommandMetadata;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.StringProvider;

import com.systemsjr.jrbbf.common.ExpenseManagerScreen;
import com.systemsjr.jrbbf.ExpenseUI;

public class ExpenseTypeScreen extends ExpenseManagerScreen {
	
	private ExpenseType expenseType;
	private boolean newExpenseType;
    private ManageExpenseType sqlManager;
    private EditField typeField;
    private EditField codeField;
    private int id;
    
	/**
	 * 
	 */
	public ExpenseTypeScreen(ExpenseType expenseType, ManageExpenseType sqlManager, boolean newType) {
		//super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
		setTitle("Expense Type Manager");
		
		this.expenseType = expenseType;
		this.sqlManager = sqlManager;
		this.newExpenseType = newType;
		
		codeField = new EditField("Code:", expenseType.getCode());
		typeField = new EditField("Type:", this.expenseType.getExpenseType());
		
		add(codeField);
		add(typeField);
	}
	
	public ExpenseTypeScreen(ManageExpenseType sqlManager2) {
		//super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
		setTitle("Expense Type Manager");
		
		this.sqlManager = sqlManager2;
		//this.db = sqlManager.
		
		this.expenseType = new ExpenseType();
		codeField = new EditField("Code:", expenseType.getCode());
		typeField = new EditField("Type:", this.expenseType.getExpenseType());
		
		add(codeField);
		add(typeField);
	}

	/**
     * @see MainScreen#makeMenu(Menu, int)
     */
    protected void makeMenu(Menu menu, int instance)
    {
        MenuItem save = new MenuItem(new StringProvider("Save"), 0x230010, 0);
        save.setCommand(new Command(new CommandHandler() 
        {
            /**
             * @see net.rim.device.api.command.CommandHandler#execute(ReadOnlyCommandMetadata, Object)
             */
            public void execute(ReadOnlyCommandMetadata metadata, Object context) 
            {
                onSave();             
            }
        }));
        menu.add(save);
        menu.add(exipApp());
        menu.add(viewExpenseTypes());
    }
       

    /**
     * 
     */
    protected boolean onSave()
    {
    	// Item name is mandatory as it will be used as a node description by the TreeField 
        if(codeField.getText().equals("")){
            Dialog.alert("Expense Type entry must have a code");
            codeField.setFocus();
            return false;
        }
        else if(typeField.getText().equals("")){

            Dialog.alert("Expense Type entry must have a name");
            typeField.setFocus();
            return false;
        }
    	
        else
        {
            String code = expenseType.getCode();
            String type = expenseType.getExpenseType();
            
            boolean edited = false;
        
            // Check whether fields have been edited           
            if(codeField.isDirty() || typeField.isDirty())
            {
                code = codeField.getText();
                expenseType.setCode(code);
                edited = true;
            
                type = typeField.getText();                
                expenseType.setExpenseType(type);
                edited = true;
                
                // If this is a new expense type, the id will be 0
                if(expenseType.getId() == 0){
                	ExpenseUI.errorDialog("new expense type ");
                	expenseType = sqlManager.add(expenseType);
                	ExpenseUI.errorDialog("Saved " + expenseType.getId());
                } else{
                    // Update the existing database record
                    sqlManager.update(expenseType);
                }
            }
        
            /*if(newExpenseType)
            {
                // Add a new item to the database 
                expenseType = sqlManager.add(expenseType);
                if(expenseType.getId() > -1)
                {
                    ExpenseUI.errorDialog("Saved " + expenseType.getId());
                }    
            } 
            else
            {
                if(edited)
                {
                    // Update the existing database record
                    sqlManager.update(expenseType);
                }
            }  */          
            
            return edited;
        }
    }
}
